// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import "babel-polyfill"
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import axios from 'axios'
// 解决Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event han
import 'default-passive-events'
import vueGridLayout from 'vue-grid-layout';
import md5 from "js-md5";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "@/assets/base.scss"
import "@/assets/font/iconfont.css"
import "../src/assets/element-variables.scss"

import {formatTime} from './functions/date'
import OdinToolPc from 'odin-tool-pc';
Vue.use(OdinToolPc); 

// Vue.use(FormMaking)
import globalComponents from "@/components/index"
Vue.use(globalComponents)
// ----------------------------自定义表单组件end
Vue.use(ElementUI);
Vue.use(vueGridLayout);

Vue.config.productionTip = false

// 引入axios，并加到原型链中
import api from './functions/request';
import { baseURL } from './config'
Vue.prototype.baseURL = baseURL;

Vue.prototype.formatTime = formatTime;
Vue.prototype.$api = api;
Vue.prototype.$md5 = md5;
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
//配置接口地址
axios.defaults.baseURL = baseURL;

axios.defaults.timeout = 30000; //响应时间
// ----------------------------------------------添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// ---------------添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  if (response.data.retHead && response.data.retHead.code == "0801005") {
    Vue.prototype.$message.error("Token不存在，请重新登录！");
    router.push("/login")
    return
  }
  if (response.data.retHead &&response.data.retHead.code == "0801006") {
    Vue.prototype.$message.error("Token验证异常，请重新登录！");
    router.push("/login")
    return
  }
  if (response.data.retHead &&response.data.retHead.code == "0801008") {
    Vue.prototype.$message.error("Token为空，请重新登录！");
    router.push("/login")
    return
  }
  if (response.data.retHead &&response.data.retHead.code == "0801009") {
    Vue.prototype.$message.error("Token超时，请重新登录！");
    router.push("/login")
    return
  }
  if (response.data.retHead && response.data.retHead.code == "9999999") {
    Vue.prototype.$message.error("系统异常，请重新登录！");
    // router.push("/login")
    // return
  }
  return response;
}, function (error) {
  return Promise.reject(error);
})

//-----------------路由安全守卫-----------------
router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem("token")
  if (to.path != "/login" && !token) {
    next({ path: "/login" })
  } else {
    next()
  }
});
router.afterEach(router => {
  if (router.path != "/login" && router.name != "404" && router.meta.affix) {
    store.commit("visitedPathAdd", router)
  }
});

Vue.prototype.padDate = function (va) {
  va = va < 10 ? '0' + va : va;
  return va;
}
Vue.prototype.changeInput = function (va) {
  if (!va) {
    return;
  }
  var obj = document.getElementsByTagName(va);
  if (obj) {
    for (var i = 0; i < obj.length; i++) {
      if (obj[i].getAttribute("type") == 'text' && !obj[i].getAttribute("placeholder")) {
        obj[i].setAttribute("placeholder", "请输入");
      }
    }
  }
  return true;
}

Vue.prototype.formatDate = function (date, format) {
  if (date == null || date == '') {
    return;
  } else {
    var date1 = new Date(date);
    let h = date1.getHours() < 10 ? "0" + date1.getHours() : date1.getHours();
    let m = date1.getMinutes() < 10 ? "0" + date1.getMinutes() : date1.getMinutes();
    let s = date1.getSeconds() < 10 ? "0" + date1.getSeconds() : date1.getSeconds();
    if (format == "yyyy-MM-dd" || format == "yyyy-mm-dd") {
      return date1.getFullYear() + '-' + this.padDate((date1.getMonth() + 1)) + '-' + this.padDate(date1.getDate());
    }
    if (format == "yyyymmdd" || format == "yyyyMMdd") {
      return date1.getFullYear() + this.padDate((date1.getMonth() + 1)) + this.padDate(date1.getDate());
    }
    if (format == "yyyymm" || format == "yyyyMM") {
      return date1.getFullYear() + this.padDate((date1.getMonth() + 1));
    }
    if (format == "yyyy-MM-dd HH:mm:ss" || format == "yyyy-mm-dd hh:mm:ss") {
      return date1.getFullYear() + '-' + this.padDate((date1.getMonth() + 1)) + '-' + this.padDate(date1.getDate()) + " " + h + ":" + m + ":" + s;
    }
    return date1;
  }

}

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

